<?php
pathnode::heritage('foldernode',2);
/**
*
* @author J.Ducastel <jducastel@jducastel.fr>
* @version
*/
class folderNode extends pathNode {
/*------------------------------------------------------------------------------
                                                                     constructor
------------------------------------------------------------------------------*/
	/**
	*
	* @access
	* /
	function __construct($path) {
		parent::__construct($path);
	}/**/
/*------------------------------------------------------------------------------
                                                                  public methods
------------------------------------------------------------------------------*/
	/**
	* detects if path applies to self class
	* @param string $path
	* @access public
	* @return bool
	*/
	public function selfDetector($path) {
		return is_dir($path);
	}/**/

	/**
	* execute regular expression search on children nodes names and returns matches
	* @param string $regex
	* @todo recursion
	* @access public
	* @return array of pathNode derived instances
	*/
	public function regexSearchName($regex,$recursive=false) {
		if (!$children=$this->getChildren()) {
			//trigger_error("foldernode->regexSearchName($regex) : folder {$this->path} has no children");
			return false;
		}
		// print_r($children);
		//echo count($children);
		$matches=array();
		for ($f=0; $f<count($children); $f++) {
			if (is_a($children[$f],'foldernode')) {
				if (!$recursive)
					continue;
				$childMatches=$children[$f]->regexSearchName($regex,$recursive);
				$matches=array_merge($matches,$childMatches);
			}
			//$child=&$children[$f];
			$basename=$children[$f]->getbasename();
			if (ereg($regex,$basename))
				$matches[]=&$children[$f];
			// else echo "<p>$basename doesn't match $regex</p>";
		}
		return $matches;
	}/**/

	/**
	* sets node children if necessary and returns them
	* @access public
	* @return array of pathNode derived instances
	*/
	public function & getChildren() {
		$false=false;
		if (!$this->exists()) return $false;
		if (!$this->children) {
			$d=dir($this->absolute_path);
			while ($path=$d->read()) {
				// avoiding path starting by .
				if (substr($path,0,1)=='.') continue;
				$node=&pathNode::getNode($this->absolute_path.'/'.$path);
				// print_r($node);
				//$node->setParent($this);
				$this->children[]=&$node;
			}
		}
		return $this->children;
	}/**/

	/**
	* return flat array of descendants
	* = children, grandchildren etc
	* @param
	* @access public
	* @return array
	*/
	public function & getDescendants() {
		return $this->regexSearchName('.*',true);
	}/**/

	/**
	* does the folder exists ?
	* @param
	* @access public
	* @return bool
	*/
	public function exists() {
		return file_exists($this->path);
	}/**/

	/**
	* creates dir if not exists
	* @param int $mode
	* @access public
	* @return bool
	*/
	public function create($mode=0777) {
		return mkdir($this->path,$mode);
	}/**/

	/**
	*
	* @param
	* @access public
	* @return
	* /
	function () {

	}/**/
/*------------------------------------------------------------------------------
                                                                      properties
------------------------------------------------------------------------------*/
	/**
	* @var type desc
	*/
	//var $type='folder';
	protected $children;
/*------------------------------------------------------------------------------
                                                                 private methods
------------------------------------------------------------------------------*/


	/**
	*
	* @param
	* @access private
	* @return
	* /
	function () {

	}/**/
}
?>
